//用户数据层
//@author likai

package dao

import (
	"admin-go-api/api/entity"
	"admin-go-api/common/util"
	"admin-go-api/pkg/db"
	"time"
)

// SysAdminDetail 从数据库获取用户详情
func SysAdminDetail(dto *entity.LoginDto) (sysAdmin entity.SysAdmin) {
	username := dto.Username
	db.Db.Where("username = ?", username).First(&sysAdmin)
	return sysAdmin
}

// GetSysAdminByUsername 根据用户名查询用户
func GetSysAdminByUsername(username string) (sysAdmin entity.SysAdmin) {
	db.Db.Where("username = ?", username).First(&sysAdmin)
	return sysAdmin
}

// CreateSysAdmin 新增用户
func CreateSysAdmin(dto entity.AddSysAdminDto) bool {
	// 查询是否存在该用户名
	sysAdminByUsername := GetSysAdminByUsername(dto.UserName)
	if sysAdminByUsername.ID > 0 {
		return false
	}
	addSysAdmin := entity.SysAdmin{
		PostId:     dto.PostId,
		DeptId:     dto.DeptId,
		Username:   dto.UserName,
		Password:   util.EncryptionMd5(dto.Password),
		Nickname:   dto.Nickname,
		Status:     dto.Status,
		Email:      dto.Email,
		Phone:      dto.Phone,
		Note:       dto.Note,
		CreateTime: util.HTime{Time: time.Now()},
	}
	// 将前端传过来的新增用户信息插入到用户数据表中
	tx := db.Db.Create(&addSysAdmin)
	// 获取新增用户的id信息
	sysAdminExist := GetSysAdminByUsername(dto.UserName)
	// 通过用户-角色关联表  来实现用户的角色赋予
	var adminRole entity.SysAdminRole
	adminRole.AdminId = sysAdminExist.ID // 根据查询的新增用户id信息赋值给中间表的用户id
	adminRole.RoleId = dto.RoleId        // 根据传入的角色id赋值给中间表的角色id
	db.Db.Create(&adminRole)
	if tx.RowsAffected > 0 {
		return true
	}
	return false
}

// GetSysAdminInfo 根据id查询用户详情
func GetSysAdminInfo(Id int) (sysAdminInfo entity.SysAdminInfo) {
	db.Db.Table("sys_admin").
		Select("sys_admin.*, sys_admin_role.role_id").
		Joins("LEFT JOIN sys_admin_role ON sys_admin_role.admin_id = sys_admin.id").
		Joins("LEFT JOIN sys_role ON sys_role.id = sys_admin_role.role_id").
		First(&sysAdminInfo, Id)
	return sysAdminInfo
}

// UpdateSysAdmin 修改用户
func UpdateSysAdmin(dto entity.UpdateSysAdminDto) (sysAdmin entity.SysAdmin) {
	db.Db.First(&sysAdmin, dto.Id)
	if dto.Username != "" {
		sysAdmin.Username = dto.Username
	}
	sysAdmin.PostId = dto.PostId
	sysAdmin.DeptId = dto.DeptId
	sysAdmin.Status = dto.Status
	if dto.Nickname != "" {
		sysAdmin.Nickname = dto.Nickname
	}
	if dto.Phone != "" {
		sysAdmin.Phone = dto.Phone
	}
	if dto.Email != "" {
		sysAdmin.Email = dto.Email
	}
	if dto.Note != "" {
		sysAdmin.Note = dto.Note
	}
	// 将传入的参数赋值给修改用户
	db.Db.Save(&sysAdmin)
	// 由于用户的roleID只能通过admin_role中间表修改
	var adminRole entity.SysAdminRole
	// 先删除该admin_id的用户的role，重新赋值
	db.Db.Where("admin_id = ?", dto.Id).Delete(&entity.SysAdminRole{})
	adminRole.AdminId = dto.Id
	adminRole.RoleId = dto.RoleId
	db.Db.Create(&adminRole)
	return sysAdmin
}

// DeleteSysAdminById 根据id删除用户
func DeleteSysAdminById(dto entity.SysAdminIdDto) {
	// db.Db.First(&entity.SysAdmin{}, dto.Id)
	// 删除用户
	db.Db.Delete(&entity.SysAdmin{}, dto.Id)
	// 删除用户对应的角色
	db.Db.Where("admin_id = ?", dto.Id).Delete(&entity.SysAdminRole{})
}

// UpdateSysAdminStatus 修改用户状态
func UpdateSysAdminStatus(dto entity.UpdateSysAdminStatus) {
	var sysAdmin entity.UpdateSysAdminStatus
	db.Db.First(&sysAdmin, dto.Id)
	sysAdmin.Status = dto.Status
	db.Db.Save(&sysAdmin)
}

// ResetSysAdminPassword 重置密码
func ResetSysAdminPassword(dto entity.ResetSysAdminPasswordDto) {
	var sysAdmin entity.SysAdmin
	db.Db.First(&sysAdmin, dto.Id)
	sysAdmin.Password = util.EncryptionMd5(dto.Password)
	db.Db.Save(&sysAdmin)
}

// GetSysAdminList 分页查询用户列表
func GetSysAdminList(PageSize, PageNum int, Username, Status, BeginTime, EndTime string) (sysAdminVo []entity.SysAdminVo, count int64) {
	curDb := db.Db.Table("sys_admin").
		Select("sys_admin.*, sys_post.post_name, sys_role.role_name,sys_dept.dept_name").
		Joins("LEFT JOIN sys_post ON sys_admin.post_id = sys_post.id").
		Joins("LEFT JOIN sys_admin_role ON sys_admin.id =sys_admin_role.admin_id").
		Joins("LEFT JOIN sys_role ON sys_role.id = sys_admin_role.role_id").
		Joins("LEFT JOIN sys_dept ON sys_admin.dept_id = sys_dept.id")
	if Username != "" {
		curDb = curDb.Where("sys_admin.username = ?", Username)
	}
	if Status != "" {
		curDb = curDb.Where("sys_admin.status = ?", Status)
	}
	if BeginTime != "" && EndTime != "" {
		curDb = curDb.Where("sys_admin.create_time BETWEEN ? AND ?", BeginTime,
			EndTime)
	}
	curDb.Count(&count)
	curDb.Limit(PageSize).Offset((PageNum - 1) *
		PageSize).Order("sys_admin.create_time DESC").Find(&sysAdminVo)
	return sysAdminVo, count
}

// UpdatePersonal 修改个人信息
func UpdatePersonal(dto entity.UpdatePersonalDto) (sysAdmin entity.SysAdmin) {
	db.Db.First(&sysAdmin, dto.Id)
	if dto.Icon != "" {
		sysAdmin.Icon = dto.Icon
	}
	if dto.Username != "" {
		sysAdmin.Username = dto.Username
	}
	if dto.Nickname != "" {
		sysAdmin.Nickname = dto.Nickname
	}
	if dto.Phone != "" {
		sysAdmin.Phone = dto.Phone
	}
	if dto.Email != "" {
		sysAdmin.Email = dto.Email
	}
	db.Db.Save(&sysAdmin)
	return sysAdmin
}

// UpdatePersonalPassword 修改个人密码
func UpdatePersonalPassword(dto entity.UpdatePersonalPasswordDto) (sysAdmin entity.SysAdmin) {
	db.Db.First(&sysAdmin, dto.Id)
	sysAdmin.Password = dto.NewPassword
	db.Db.Save(&sysAdmin)
	return sysAdmin
}
